package com.morder.activity;

import com.morder.R;
import com.morder.application.BuildConfig;
import com.morder.util.FileUtils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity implements OnClickListener {

  private Button mBack;
  private Button mSendVerifyCode;
  private EditText mobileNum;
  private BroadcastReceiver mReceiver;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register_activity);
    mBack = (Button) findViewById(R.id.back_login);
    mSendVerifyCode = (Button) findViewById(R.id.send_verify_code);
    mBack.setOnClickListener(this);
    mSendVerifyCode.setOnClickListener(this);
    mobileNum = (EditText) findViewById(R.id.mobile_num);
    
    interfaceRefresh();
    registerReceiver(mReceiver, new IntentFilter(BuildConfig.REGISTER_SUCCESS));
  }

  private void interfaceRefresh() {
    mReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("msg");
        if (msg.equals("close")) {
          finish();
        }
      }
    };
  }
  
  @Override
  protected void onDestroy() {
    super.onDestroy();
    registerReceiver(mReceiver, new IntentFilter(BuildConfig.REGISTER_SUCCESS));
    unregisterReceiver(mReceiver);
  }
  
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.back_login:
      finish();
      break;
    case R.id.send_verify_code:
      if (mobileNum != null) {
        String number = mobileNum.getText().toString();
        if (FileUtils.checkMobileNO(number)) {
          Intent intent = new Intent(this, VerifyNumberActivity.class);
          startActivity(intent);
        } else {
          Toast.makeText(this, "号码输入错误", Toast.LENGTH_SHORT).show();
        }
      }
      break;
    default:
      break;
    }
  }
}
